//
//  ProductDetailsViewController.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/29.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "ProductDetailsViewController.h"
#import "homeVCCell.h"
#import "SUNScrollImage.h"
#import "ProductDetailsView.h"
#import "CycleScrollView.h"
#import "productSaveView.h"
#import "PurchaseSubmitViewController.h"
#import "commodityDetailsView.h"
#import "SVGloble.h"
#import "DCPicScrollView.h"
#import "DetailsImagesView.h"
#import "LoginViewController.h"
@interface ProductDetailsViewController ()<ProductSaveViewDelegate,LoginViewControllerDelegate>{
    NSArray *colors;
    NSArray *_testArray;
    NSMutableArray *imageArray;
    int selectIndex;
}
@property (nonatomic,strong) UIScrollView *cellScroll;
@property (nonatomic,strong) CycleScrollView *mainScorllView;
@property (nonatomic,strong) UIView *selectLineView;
@property (nonatomic,strong) NSMutableArray *imageArray;
@property (nonatomic,strong) NSMutableDictionary *supplyData;
@end

@implementation ProductDetailsViewController
@synthesize supply,image480Array,cellArray,originalArray,viewArray,mainScorllView,Scroll,selectLineView,imageArray,cellScroll,supplyData;
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品详情";
    [self.leftBtn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    TIME
    HEADDIC(nowTime, @"/supply/getdetail");
//    dispatch_async(dispatch_get_main_queue(), ^{
    self.supplyData= [NSMutableDictionary dictionary];
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    [body setObject:self.supply.material_id forKey:@"material_id"];
    [body setObject:self.supply.home_id forKey:@"supply_id"];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:HEAD forKey:@"head"];
    [dic setObject:body forKey:@"body"];
    requestManage *request = [[requestManage alloc]init];
    NSString *result = [request post:APPIP postDataDic:dic error:nil];
    NSMutableDictionary *supplyData1 = [result objectFromJSONString];
    self.supplyData = [supplyData1 objectForKey:@"data"];
    self.image480Array = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];
    self.cellArray = [NSMutableArray array];
    self.originalArray = [NSMutableArray array];
    self.viewArray = [NSMutableArray array];
    NSMutableDictionary *pics = [self.supplyData objectForKey:@"pics"];
    if (pics !=NULL) {
        NSMutableArray *thumb480x480 = [pics objectForKey:@"thumb480x480"];
        if (thumb480x480 !=NULL || [thumb480x480 count]!=0) {
            self.image480Array = thumb480x480;
        }
        NSMutableArray *original = [pics objectForKey:@"original"];
        if (original !=NULL || [original count]!=0) {
            self.originalArray = original;
        }
    }
    [self.view setBackgroundColor:UIColorFromRGB(0xf3f2f2)];
    
    ProductDetailsView *headerView = [[ProductDetailsView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 136)];
    headerView.tag = 400;
    [headerView drawDataCellView:self.supplyData];
    [headerView setBackgroundColor:[UIColor whiteColor]];
    if ([[self.supplyData objectForKey:@"audit_status"] integerValue]==0) {
        [headerView.purchaseBtn setTitle:@"审核中" forState:0];
        [headerView.purchaseBtn setBackgroundColor:REDColor];
    }else if ([[self.supplyData objectForKey:@"audit_status"] integerValue]==1){
        [headerView.purchaseBtn addTarget:self action:@selector(purchaseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [headerView.purchaseBtn setTitle:@"未通过" forState:0];
        [headerView.purchaseBtn setBackgroundColor:UIColorFromRGB(0xbfbfbf)];
    }
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = CGRectMake(0, 64, SWIDTH, VHEIGHT); // frame中的size指UIScrollView的可视范围
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor clearColor];
    self.Scroll = scrollView;
    [self.view addSubview:self.Scroll];
    // 设置UIScrollView的滚动范围（内容大小）
    NSArray *arr = @[@"产品参数",
                     @"产品展示"];
    UIView *upView = [self createViewFrame:CGRectMake(0, 64, SWIDTH, 44) bgColor:[UIColor whiteColor] tag:20];
    [self.Scroll addSubview:upView];
    if ([image480Array count]!=0) {
        if ([image480Array count]==1) {
            if (cellScroll ==nil) {
                self.cellScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 192)];
                [self.cellScroll setContentSize:CGSizeMake(SWIDTH*[self.image480Array count], 192)];
            }
            NSMutableArray *viewsArray = [@[] mutableCopy];
            for (int c = 0; c < [self.image480Array count]; c++) {
                NSString *banner = [self.image480Array objectAtIndex:c];
                SUNScrollImage * temp = [[SUNScrollImage alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 192)];
                temp.imageButton.tag = c+30;
                [temp.imageButton setHidden:YES];
                if (![Unity isBlankString:banner]) {
                    [temp.imagev setImageURL:[NSURL URLWithString:banner]];
                }
                temp.backgroundColor = [UIColor clearColor];
                [viewsArray addObject:temp];
                [self.cellScroll addSubview:temp];
            }
            [self.Scroll addSubview:self.cellScroll];
        }else{
            DCPicScrollView  *picView = [[DCPicScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 192) WithImageNames:self.image480Array];
            picView.placeImage = [UIImage imageNamed:@"缺省图"];
            [picView setImageViewDidTapAtIndex:^(NSInteger index) {
                printf("你点到我了😳index:%zd\n",index);
            }];
            picView.AutoScrollDelay = 1.0f;
            [self.Scroll addSubview:picView];
        }
        headerView.frame = CGRectMake(0, 192, SWIDTH, 146);
        upView.frame =CGRectMake(0, 348, SWIDTH, 344);
        [self.Scroll setContentSize:CGSizeMake(SWIDTH, 670)];
    }else{
        if (cellScroll ==nil) {
            self.cellScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 192)];
            [self.cellScroll setContentSize:CGSizeMake(SWIDTH*[self.image480Array count], 192)];
        }
        SUNScrollImage * temp = [[SUNScrollImage alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 192)];
        temp.imageButton.tag = 30;
        [temp.imageButton setHidden:YES];
        temp.backgroundColor = [UIColor clearColor];
        [self.cellScroll addSubview:temp];
        [self.Scroll addSubview:self.cellScroll];
//        scrollView.contentSize = CGSizeMake(SWIDTH, 462);
//        upView.frame =CGRectMake(0, 132, SWIDTH, 344);
//        [self.Scroll setContentSize:CGSizeMake(SWIDTH, 510)];
        headerView.frame = CGRectMake(0, 192, SWIDTH, 146);
        upView.frame =CGRectMake(0, 348, SWIDTH, 344);
        [self.Scroll setContentSize:CGSizeMake(SWIDTH, 670)];
    }
    UIView *upView1 = [self createViewFrame:CGRectMake(0, 44, SWIDTH, SWIDTH +100) bgColor:[UIColor whiteColor] tag:21];
    [upView addSubview:upView1];
    
    UIView *tempLineView = [[UIView alloc] initWithFrame:CGRectMake(10, 43, (SWIDTH /2)-20, 1)];
    self.selectLineView = tempLineView;
    [self.selectLineView setBackgroundColor:UIColorFromRGB(0X177BD0)];
    [upView addSubview:self.selectLineView];
    for(NSInteger i=0;i<[arr count];i++)
    {
        UIButton *button = [self createButtonFrame:CGRectMake((SWIDTH /2)*i, 0, (SWIDTH /2), 44) title:[arr objectAtIndex:i] viewController:self selector:@selector(selectClick:) tag:i+110];
        [button setTitleColor:GRAYColor forState:0];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [upView addSubview:button];
        if (i==0) {
            self.selectLineView.frame = CGRectMake(10, 43, (SWIDTH /2)-20, 1);
            [self selectClick:button];
        }
    }
    
    UIView *line = [self createViewFrame:CGRectMake((SWIDTH/2)-1, 4, 1, 44 - 8) bgColor:[UIColor grayColor] tag:0];
    line.alpha = 0.2;
    [upView addSubview:line];
    [self.Scroll addSubview:headerView];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    
    
    productSaveView *saveView = [[productSaveView alloc]initWithFrame:CGRectMake(0, SHEIGHT - 43, SWIDTH, 43)];
    [saveView setTag:401];
    saveView.delegate = self;
    [saveView setBackgroundColor:[UIColor whiteColor]];
    saveView.supply_id = [self.supplyData objectForKey:@"id"];
    saveView.material_id = [self.supplyData objectForKey:@"material_id"];
    saveView.is_favor = [self.supplyData objectForKey:@"is_favor"];
    [saveView changeSaveState];
    [self.view addSubview:saveView];
//    });
    
    // Do any additional setup after loading the view.
}
-(void)isLogin{
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    loginVC.title = @"登录";
//    loginVC.baseVC = self;
//    [self.navigationController pushViewController:loginVC animated:YES];
    [self presentViewController:loginVC animated:YES completion:^{
        loginVC.viewState = 1;
    }];
}
-(void)selectClick:(UIButton *)sender{
    UIView *view = (UIView *)[self.view viewWithTag:20];
    UIView *view1 = (UIView *)[self.view viewWithTag:21];
//    for (int i = 110; i<2; i++) {
//        UIButton *button = (UIButton*)[view viewWithTag:i];
//        [button setTitleColor:UIColorFromRGB(0x959595) forState:0];
//    }
    for (UIView *viewButton in view.subviews) {
        if ([viewButton isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)viewButton;
            [button setTitleColor:GRAYColor forState:0];
        }
    }
    [UIView animateWithDuration:0.4 animations:^{
        [sender setTitleColor:BLUEColor forState:UIControlStateNormal];
        self.selectLineView.frame = CGRectMake(CGRectGetMinX(sender.frame)+10, 43, (SWIDTH/2)-20, 1);
    }];
    if (sender.tag == 110) {
//        for (int i = 200; i <[self.originalArray count] +200; i++) {
//            EGOImageView *egoImage = (EGOImageView *)[view1 viewWithTag:i];
//            egoImage.image = nil;
//            [egoImage removeFromSuperview];
//            egoImage = nil;
//        }
        UIView *imgTempView = (UIView*)[view1 viewWithTag:333];
        if (imgTempView) {
            for (UIView *EgoImg in [imgTempView subviews]) {
                if ([EgoImg isKindOfClass:[EGOImageView class]]) {
                    EGOImageView *tempEgo = (EGOImageView *)EgoImg;
                    [tempEgo removeFromSuperview];
                    tempEgo = nil;
                }
            }
            [imgTempView removeFromSuperview];
            imgTempView = nil;
        }
        
//        DetailsImagesView *imagesView = (DetailsImagesView *)[view1 viewWithTag:333];
//        if (imagesView) {
//            [imagesView removeFromSuperview];
//            imagesView = nil;
//        }
        
        NSString *address = @"";
        [NSString stringWithFormat:@"%@%@",self.supply.province_name,self.supply.city_name];
        if ([Unity isBlankString:self.supply.province_name]&&[Unity isBlankString:self.supply.city_name]) {
            address = @"";
        }else{
            if (![Unity isBlankString:self.supply.province_name]) {
                address = [address stringByAppendingString:[NSString stringWithFormat:@"%@",self.supply.province_name]];
            }
            if (![Unity isBlankString:self.supply.city_name]) {
                address = [address stringByAppendingString:[NSString stringWithFormat:@"%@",self.supply.city_name]];
            }
        }

        if ([Unity isBlankString:self.supply.brand_name]) {
            self.supply.brand_name = @"";
        }
        if ([Unity isBlankString:self.supply.material_name]){
            self.supply.material_name = @"";
        }
        if ([Unity isBlankString:self.supply.eplevel]){
            self.supply.eplevel = @"";
        }
        if ([Unity isBlankString:self.supply.veneer]){
            self.supply.veneer = @"";
        }
        if ([Unity isBlankString:self.supply.texture]){
            self.supply.texture = @"";
        }
        if ([Unity isBlankString:self.supply.length]){
            self.supply.length = @"";
        }
        if ([Unity isBlankString:self.supply.width]){
            self.supply.width = @"";
        }
        if ([Unity isBlankString:self.supply.thickness]){
            self.supply.thickness = @"";
        }
        if ([Unity isBlankString:self.supply.stuff]){
            self.supply.stuff = @"";
        }
        if ([Unity isBlankString:self.supply.model]){
            self.supply.model = @"";
        }
        if ([Unity isBlankString:self.supply.gram]){
            self.supply.gram = @"";
        }
        NSArray *arr1 = [NSArray arrayWithObjects:self.supply.brand_name,self.supply.material_name,self.supply.eplevel,self.supply.veneer,self.supply.texture,self.supply.length,self.supply.width,self.supply.thickness,address, nil];
        
        NSArray *arr2 = [NSArray arrayWithObjects:self.supply.brand_name,self.supply.material_name,self.supply.eplevel,self.supply.texture,self.supply.stuff,self.supply.length,self.supply.width,self.supply.thickness,address, nil];
        
        NSArray *arr3 = [NSArray arrayWithObjects:self.supply.brand_name,self.supply.material_name,self.supply.model,self.supply.texture,self.supply.gram,self.supply.length,self.supply.width,address, nil];
        commodityDetailsView *cellView = nil;
        cellView = [[commodityDetailsView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 300)];
        cellView.tag = 22;
        cellView.material_id = [self.supply.material_id intValue];
        int material_id = [self.supply.material_id intValue];
        if (material_id >= 100000000 && material_id <200000000) { //板材
            [cellView drawDetailsView:0 detailsInformation:arr1];
        }else if (material_id >=200000000 && material_id <= 300000000){ //装饰纸
            [cellView drawDetailsView:2 detailsInformation:arr3];
        }else if (material_id >=300000000 && material_id < 400000000){ //门板
            [cellView drawDetailsView:1 detailsInformation:arr2];
        }
        [view1 addSubview:cellView];
        view1.frame = CGRectMake(0, 44, SWIDTH, SWIDTH +100);
        [self.Scroll setContentSize:CGSizeMake(SWIDTH, 670)];
        
    }else{
        commodityDetailsView *cellView = (commodityDetailsView *)[view1 viewWithTag:22];
        if (cellView) {
            [cellView removeFromSuperview];
            cellView = nil;
        }
        if ([self.originalArray count]!=0) {
//            DetailsImagesView *imagesView = [[DetailsImagesView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, SWIDTH+50)];
//            imagesView.dataList = self.originalArray;
//            imagesView.tag = 333;
//            imagesView.backgroundColor = [UIColor whiteColor];
//            [view1 addSubview:imagesView];
            
            UIView *imgBgView = [self createViewFrame:CGRectMake(0, 10, SWIDTH, 210*[self.originalArray count]) bgColor:[UIColor clearColor] tag:333];
            [view1 addSubview:imgBgView];
            for (int i = 0; i <[self.originalArray count]; i++) {
                EGOImageView *imageView = [[EGOImageView alloc]initWithPlaceholderImage:[UIImage imageNamed:@"缺省图"]];
                imageView.frame = CGRectMake(0, i*210, SWIDTH, 200);
                [imageView setImageURL:[NSURL URLWithString:[self.originalArray objectAtIndex:i]]];
                imageView.contentMode = UIViewContentModeScaleToFill;
                imageView.tag = 200+i;
                [imgBgView addSubview:imageView];
            }
            view1.frame = CGRectMake(0, 44, SWIDTH, 460+210*[self.originalArray count]);
            [self.Scroll setContentSize:CGSizeMake(SWIDTH, 460+210*[self.originalArray count])];
        }
    }
}

-(void)purchaseBtnClick:(UIButton *)sender{
    NSLog(@"我要采购");
    PurchaseSubmitViewController *submitViewController = [[PurchaseSubmitViewController alloc]init];
    submitViewController.supply = self.supply;
    submitViewController.title = @"我要采购";
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"0"] || [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] == nil) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        loginVC.baseVC = submitViewController;
        loginVC.delegate = self;
//        [self.navigationController pushViewController:loginVC animated:YES];
        [self presentViewController:loginVC animated:YES completion:^{
           loginVC.viewState = 1;
        }];
        return;
    }
    [self.navigationController pushViewController:submitViewController animated:YES];
}
-(void)pushToNextViewController:(id)viewController{
    BaseViewController *baseVC = (BaseViewController *)viewController;
    [self.navigationController pushViewController:baseVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
